package com.eastfair.venueservice.service.impl;

import com.eastfair.common.dto.AttachmentFileDTO;
import com.eastfair.common.vo.AttachmentFileVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.api.AttachmentFileFeign;
import com.eastfair.venueservice.service.VenueManageAttachFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * common service 附件接口.
 *
 * @author dqq
 * @date 2022 /11/29
 */
@Service
@Slf4j
public class VenueManageAttachFileServiceImpl implements VenueManageAttachFileService {

    @Resource
    private AttachmentFileFeign attachmentFileFeign;

    @Override
    public List<AttachmentFileVO> queryAttachmentFile(AttachmentFileDTO attachmentFileDTO) {
        log.info("queryAttachmentFile - 查询附件, attachmentFileDTO={}", attachmentFileDTO);
        // 查询布撤展管理附件
        R<List<AttachmentFileVO>> result = attachmentFileFeign.queryAttachmentFile(attachmentFileDTO);
        log.info("queryAttachmentFile - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public List<AttachmentFileVO> queryByBusinessId(Long businessId, String businessType) {
        // 查询布撤展管理附件
        AttachmentFileDTO attachmentFileDTO = new AttachmentFileDTO();
        attachmentFileDTO.setBusinessId(businessId);
        attachmentFileDTO.setBusinessType(businessType);
        return queryAttachmentFile(attachmentFileDTO);
    }

    @Override
    public List<AttachmentFileVO> queryByBusinessIds(List<Long> businessIds, String businessType) {
        AttachmentFileDTO attachmentFileDTO = new AttachmentFileDTO();
        attachmentFileDTO.setBusinessIdList(businessIds);
        attachmentFileDTO.setBusinessType(businessType);
        return queryAttachmentFile(attachmentFileDTO);
    }

    @Override
    public Map<Long, List<AttachmentFileVO>> queryMapByBusinessIds(List<Long> businessIds, String businessType) {
        List<AttachmentFileVO> attachmentFileList = queryByBusinessIds(businessIds, businessType);
        if (attachmentFileList == null || attachmentFileList.isEmpty()) {
            return Collections.emptyMap();
        }
        return attachmentFileList
                .stream()
                .collect(Collectors.groupingBy(AttachmentFileVO::getBusinessId));
    }

    @Override
    public List<AttachmentFileVO> queryByBusinessId(Long businessId) {
        AttachmentFileDTO attachmentFileDTO = new AttachmentFileDTO();
        attachmentFileDTO.setBusinessId(businessId);
        return queryAttachmentFile(attachmentFileDTO);
    }

    @Override
    public void saveOrUpdateAttachmentFile(List<AttachmentFileDTO> attachmentFileList) {
        log.info("saveOrUpdateAttachmentFile - 批量保存附件, attachmentFileDTOs={}", attachmentFileList);
        if (attachmentFileList == null || attachmentFileList.isEmpty()) {
            return;
        }
        R result = attachmentFileFeign.addAttachmentFile(attachmentFileList);
        log.info("saveOrUpdateAttachmentFile - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        log.info("deleteBatch - 批量删除附件, ids={}", ids);
        R result = attachmentFileFeign.deteleBatch(ids);
        log.info("deleteBatch - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
    }

    @Override
    public void deleteBatchByBusinessId(Long businessId) {
        // 查询附件列表
        List<AttachmentFileVO> attachmentFileList = queryByBusinessId(businessId);
        if (attachmentFileList == null || attachmentFileList.isEmpty()) {
            return;
        }
        // 删除布撤展管理的附件
        List<Long> attachmentFileIdList = attachmentFileList
                .stream()
                .map(AttachmentFileVO::getId)
                .collect(Collectors.toList());
        deleteBatch(attachmentFileIdList);
    }
}